﻿Imports System.IO
Imports System.Environment
Imports System.Security.Cryptography
Imports ICSharpCode.SharpZipLib

Public Class frmMain

    Private ORIGINAL_BYTES() As Byte = {&H30, &H82, &H1, &H22, &H30, &HD, &H6, &H9, &H2A, &H86, &H48, &H86, &HF7, &HD, &H1, &H1, _
                                        &H1, &H5, &H0, &H3, &H82, &H1, &HF, &H0, &H30, &H82, &H1, &HA, &H2, &H82, &H1, &H1, _
                                        &H0, &HC6, &HF3, &HDE, &HFD, &HEF, &HA9, &H62, &H9C, &HE0, &H51, &H28, &H9B, &HFB, &H46, &HBC, _
                                        &HF4, &H1A, &H3, &HD, &H6F, &H69, &HF1, &H98, &H2A, &HA6, &H35, &HB9, &H62, &HD6, &H36, &H6B, _
                                        &H64, &H53, &H87, &H93, &HD6, &HA9, &HD3, &H46, &HD7, &H93, &H4F, &H1D, &H8E, &H50, &H50, &H7D, _
                                        &H51, &H80, &HD1, &H29, &H8F, &H83, &H37, &HB6, &H67, &HFD, &HDE, &HA3, &HDF, &H2, &HDA, &H52, _
                                        &HBA, &H1C, &H3A, &H3E, &H6F, &HE4, &HB6, &HC8, &H55, &H75, &H91, &HF8, &H6B, &H2A, &H51, &H5A, _
                                        &HAD, &HF6, &H26, &H9B, &HFF, &HCF, &H67, &H15, &HA0, &HB9, &H5A, &HB8, &HCA, &HAF, &H8F, &HEF, _
                                        &H9A, &H15, &H45, &H9F, &H87, &HD0, &H82, &H89, &H55, &H45, &H91, &H7E, &H90, &H3, &H84, &H45, _
                                        &H6B, &HDF, &HEB, &HA4, &H95, &H71, &H74, &HBD, &HF, &H8B, &HF7, &HA8, &HC4, &HFA, &HD5, &H7D, _
                                        &H6F, &HFF, &H1, &HC0, &H4A, &H64, &HD2, &H73, &H2, &HF1, &H4F, &H72, &H87, &H48, &H80, &HA2, _
                                        &HC, &H9C, &H3C, &HD5, &HAD, &HBE, &HFB, &HF0, &H38, &H34, &HAF, &H25, &H10, &HEF, &H96, &HAF, _
                                        &H8C, &H3D, &HFA, &H48, &H54, &H5F, &HE4, &H11, &H43, &HA2, &H74, &HE9, &HC4, &H28, &HA9, &H6, _
                                        &H3D, &HCC, &HBD, &HC0, &HBE, &H48, &HB4, &H22, &HD6, &HD2, &H34, &HEE, &H2F, &H7, &H76, &HE7, _
                                        &H33, &H9F, &HD, &HE5, &H9E, &H34, &H8A, &HC6, &HEC, &H2B, &H75, &H15, &H3A, &H2F, &HA8, &HA6, _
                                        &H9A, &H77, &H68, &H17, &HF2, &H90, &H65, &H5B, &HEF, &H52, &H33, &HAA, &H4B, &H5, &HF3, &H8, _
                                        &H80, &HE, &HDF, &HD, &HFB, &H8B, &H67, &HE, &H17, &H54, &H25, &H9F, &H75, &HA9, &HF8, &H66, _
                                        &H28, &HEB, &H70, &H31, &H49, &HAC, &HE3, &H9D, &HB1, &H10, &HC8, &HFD, &HFD, &H8D, &H23, &H6C, _
                                        &HEF, &H2, &H3, &H1, &H0, &H1}

    Private NEW_BYTES() As Byte = {48, 130, 1, 34, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, _
                                   1, 5, 0, 3, 130, 1, 15, 0, 48, 130, 1, 10, 2, 130, 1, 1, _
                                   0, 195, 209, 50, 174, 126, 34, 190, 78, 64, 154, 236, 64, 207, 138, 16, _
                                   155, 240, 171, 53, 81, 223, 96, 90, 184, 239, 212, 42, 24, 113, 206, 11, _
                                   242, 155, 104, 235, 134, 198, 8, 218, 193, 34, 59, 116, 9, 238, 214, 11, _
                                   233, 199, 218, 3, 89, 217, 64, 78, 58, 235, 150, 31, 178, 94, 223, 97, _
                                   82, 60, 64, 187, 2, 141, 236, 249, 84, 67, 144, 113, 110, 53, 187, 25, _
                                   35, 108, 42, 213, 166, 133, 0, 24, 19, 1, 13, 143, 53, 32, 93, 144, _
                                   66, 59, 144, 50, 206, 142, 211, 120, 163, 35, 82, 14, 140, 215, 58, 165, _
                                   131, 177, 92, 161, 196, 124, 126, 146, 215, 22, 185, 12, 230, 93, 158, 223, _
                                   121, 49, 228, 184, 188, 109, 88, 67, 35, 39, 29, 40, 94, 4, 231, 28, _
                                   63, 221, 180, 74, 82, 36, 97, 215, 72, 196, 120, 175, 53, 18, 127, 118, _
                                   142, 122, 248, 224, 246, 32, 219, 143, 13, 53, 119, 123, 90, 113, 206, 177, _
                                   6, 239, 226, 144, 29, 75, 84, 141, 223, 143, 191, 248, 114, 56, 138, 209, _
                                   232, 4, 145, 225, 157, 50, 248, 118, 33, 78, 69, 171, 142, 38, 193, 202, _
                                   172, 4, 120, 21, 166, 180, 191, 201, 83, 113, 251, 186, 224, 138, 21, 190, _
                                   249, 65, 74, 60, 241, 252, 112, 112, 195, 126, 110, 109, 188, 66, 122, 227, _
                                   124, 135, 138, 230, 59, 153, 117, 18, 74, 84, 29, 161, 121, 129, 71, 157, _
                                   149, 2, 3, 1, 0, 1}

    Private JAR_START() As Byte = {&H50, &H4B, &H3, &H4}

    Private Sub btnPatch_Click(sender As System.Object, e As System.EventArgs) Handles btnPatch.Click

        Dim MinecraftPath As String = My.Application.Info.DirectoryPath & "\minecraft.exe"

        ' Step 1: Attemp to locate Minecraft.exe
        If Not File.Exists(MinecraftPath) Then
            MsgBox("Please place this patcher in the same folder as minecraft.exe.", vbOKOnly Or vbCritical)
            End
        End If

        ' Step 2: Read minecraft.exe
        Dim Minecraft() As Byte = System.IO.File.ReadAllBytes(MinecraftPath)

        ' Step 3: Check if file has been patched
        Dim TestMatch As Boolean = True
        Dim i As Integer
        For i = 0 To (Minecraft.Count - 1) - (NEW_BYTES.Count - 1)
            TestMatch = True
            For test = 0 To NEW_BYTES.Count - 1
                If Not Minecraft(i + test) = NEW_BYTES(test) Then
                    TestMatch = False
                    Exit For
                End If
            Next
            If TestMatch = True Then Exit For
        Next
        If TestMatch = True Then
            MsgBox("File is already patched.", vbInformation Or vbOKOnly)
            Exit Sub
        End If

        ' Step 4: Locate patch position
        For i = 0 To (Minecraft.Count - 1) - (ORIGINAL_BYTES.Count - 1)
            TestMatch = True
            For test = 0 To ORIGINAL_BYTES.Count - 1
                If Not Minecraft(i + test) = ORIGINAL_BYTES(test) Then
                    TestMatch = False
                    Exit For
                End If
            Next
            If TestMatch = True Then Exit For
        Next
        If Not TestMatch Then
            MsgBox("Failed to locate original Minecraft.key within Minecraft.exe.", vbOKOnly Or vbCritical)
            Exit Sub
        End If

        ' Step 5: Patch key file in memory
        For patch = i To i + (ORIGINAL_BYTES.Count - 1)
            Minecraft(patch) = NEW_BYTES(patch - i)
        Next

        ' Step 6: Find the jar file stored in the executable
        Dim MinecraftJar() As Byte = Nothing
        Dim JarLoc As Long
        For JarLoc = 0 To (Minecraft.Count - 1) - (JAR_START.Count - 1)
            TestMatch = True
            For test = 0 To JAR_START.Count - 1
                If Not Minecraft(JarLoc + test) = JAR_START(test) Then
                    TestMatch = False
                    Exit For
                End If
            Next
            If TestMatch = True Then Exit For
        Next
        If Not TestMatch Then
            MsgBox("Unable to locate Java Archive within Minecraft.exe", vbCritical Or vbOKOnly)
            Exit Sub
        End If
        ReDim MinecraftJar((Minecraft.Length - 1) - JarLoc)
        Array.Copy(Minecraft, JarLoc, MinecraftJar, 0, (Minecraft.Length - 1) - JarLoc)
        File.WriteAllBytes("test.jar", MinecraftJar)
        Exit Sub

        ' Step 7: Backup original
        File.Copy(MinecraftPath, MinecraftPath & ".old", True)

        ' Step 8: Overwrite original
        File.Delete(MinecraftPath)
        File.WriteAllBytes(MinecraftPath, Minecraft)

        MsgBox("Done.", vbOKOnly Or vbInformation)

    End Sub

    Private Function getFileMd5(ByVal filePath As String) As String
        Dim File() As Byte = System.IO.File.ReadAllBytes(filePath)
        Dim Md5 As New SHA1CryptoServiceProvider
        Dim byteHash() As Byte = Md5.ComputeHash(File)
        Return Convert.ToBase64String(byteHash)
    End Function

End Class
